package de.swm.gwt.client.utils;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;



/**
 * Ermoglicher einer Komponente die einen Clickehandler benoetigt weitere ClickHanlder zur laufzeit hinzuzufuegen.
 * 
 * @author wiese.daniel <br>
 *         copyright (C) 2011, SWM Services GmbH
 * 
 */
public class ShimClickHandler implements ClickHandler, HasClickHandlers {

	private final List<ClickHandler> clickHandlers = new ArrayList<ClickHandler>();



	@Override
	public void onClick(ClickEvent event) {
		for (ClickHandler clickHandler : clickHandlers) {
			clickHandler.onClick(event);
		}

	}



	@Override
	public void fireEvent(GwtEvent<?> event) {
		throw new IllegalArgumentException("Not supported");

	}



	@Override
	public HandlerRegistration addClickHandler(final ClickHandler handler) {
		clickHandlers.add(handler);
		return new HandlerRegistration() {

			@Override
			public void removeHandler() {
				clickHandlers.remove(handler);

			}
		};
	}

}
